//
//  NewCityLoanCell.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/12/5.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

class NewCityLoanCell: UITableViewCell {
    
    var model : GenericLoansModel! {
        didSet{
            updateData()
        }
    }
    
    fileprivate let bankImageView = UIImageView() //图片
    fileprivate let bankLabel = UILabel() //银行
    fileprivate let lenderLabel = UILabel() //放款时间
    fileprivate let mortgageLabel = UILabel() //抵押类型
    fileprivate let amountLabel = UILabel() //贷款额度
    fileprivate let highestLabel = UILabel() //最高贷款
    fileprivate let lineView = UIView()
    fileprivate let workLabel = UILabel() //职业类型
    fileprivate let interestLabel = UILabel() //利息
    fileprivate let millionLabel = UILabel() //万元
    fileprivate let dayLabel = UILabel() //日利息
    fileprivate let lineView2 = UIView()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.contentView.addSubview(bankImageView)
        
        bankLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        self.contentView.addSubview(bankLabel)
        
        lenderLabel.text = ""
        lenderLabel.textAlignment = .center
        lenderLabel.textColor = UIColor.lenderColor()
        lenderLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        self.contentView.addSubview(lenderLabel)
        
        mortgageLabel.text = ""
        mortgageLabel.textAlignment = .center
        mortgageLabel.textColor = UIColor.mortgageColor()
        mortgageLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        self.contentView.addSubview(mortgageLabel)
        
        amountLabel.font = UIFont.boldSystemFont(ofSize: RATIO(maxNum: 20))
        self.contentView.addSubview(amountLabel)
        
        highestLabel.textColor = UIColor.gray
        highestLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        self.contentView.addSubview(highestLabel)
        
        self.contentView.addSubview(lineView)
        
        workLabel.numberOfLines = 0
        workLabel.textColor = UIColor.gray
        workLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        self.contentView.addSubview(workLabel)
        
        interestLabel.adjustsFontSizeToFitWidth = true
        interestLabel.textColor = UIColor.red
        interestLabel.font = UIFont.boldSystemFont(ofSize: RATIO(maxNum: 20))
        self.contentView.addSubview(interestLabel)
        
        millionLabel.textColor = UIColor.gray
        millionLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 12))
        self.contentView.addSubview(millionLabel)
        
        dayLabel.textColor = UIColor.gray
        dayLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        self.contentView.addSubview(dayLabel)
    
        lineView2.backgroundColor = UIColor.lineColor()
        self.contentView.addSubview(lineView2)
        
    }
    
    //MARK: 更新数据
    fileprivate func updateData() {
        
        bankLabel.text = model.organization_name! + "-" + model.product_name!
        bankImageView.image = UIImage.imageWithName(KMTool.selectBankImage(bankName: model.organization_name!))
        lenderLabel.text = model.time_advange
        mortgageLabel.text = model.mortgage_show
        workLabel.text = model.identitychoose
        millionLabel.text = "/" + millionYuan
        dayLabel.text = dailyInterest
        
        if workLabel.text == nil || workLabel.text == "" {
            workLabel.text = model.identity
        }
        
        let money_min = Double(model.money_min ?? "") ?? 0
        let money_max = Double(model.money ?? "") ?? 0
        
        if money_min > 0 && money_max > 0 {
            amountLabel.text = model.money_min! + "-" + model.money! + million
            highestLabel.text = loanLimit
        }else if money_max > 0 {
            amountLabel.text = model.money! + million
            highestLabel.text = maxLoan
        }else if money_min > 0 {
            amountLabel.text = model.money_min! + million
            highestLabel.text = minLoan
        }else{
            if model.show_house_money == "1" {
                amountLabel.text = property + model.house_money! + "%"
            }else if model.show_car_money == "1" {
                amountLabel.text = automobileProduction + model.car_money! + "%"
            }
            
            highestLabel.text = minLoan
        }
        
        var interest_rate:Double = 0
        if model.rate_unit == years {
            interest_rate = Double(model.interest_rate!)!/365 * 100
        }else if model.rate_unit == month {
            interest_rate = Double(model.interest_rate!)!/30 * 100
        }else{
            interest_rate = Double(model.interest_rate!)! * 100
        }
        
        interestLabel.text = "\(String(format: "%.2f", interest_rate))" + yuan
        
        lenderLabel.layer.borderWidth = 1
        lenderLabel.layer.borderColor = UIColor.lenderColor().cgColor
        mortgageLabel.layer.borderColor = UIColor.mortgageColor().cgColor
        mortgageLabel.layer.borderWidth = 1
        lineView.backgroundColor = UIColor.lineColor()
        
        bankImageView.snp.remakeConstraints { (make) in
            make.top.equalToSuperview().offset(RATIO_H(maxNum: 20))
            make.left.equalToSuperview().offset(RATIO(maxNum: 10))
            make.width.height.equalTo(RATIO_H(maxNum: 30))
        }
        
        bankLabel.snp.remakeConstraints { (make) in
            make.centerY.equalTo(bankImageView)
            make.left.equalTo(bankImageView.snp.right).offset(RATIO(maxNum: 10))
            make.width.greaterThanOrEqualTo(10)
            make.height.equalTo(RATIO_H(maxNum: 20))
        }
        
        amountLabel.snp.remakeConstraints { (make) in
            make.top.equalTo(bankImageView.snp.bottom).offset(RATIO_H(maxNum: 20))
            make.left.equalTo(bankImageView)
            make.width.lessThanOrEqualTo(RATIO(maxNum: 80))
            make.height.equalTo(RATIO_H(maxNum: 25))
        }
        
        highestLabel.snp.remakeConstraints { (make) in
            make.top.equalTo(amountLabel.snp.bottom)
            make.left.height.equalTo(amountLabel)
            make.width.equalTo(highestLabel.text?.size(font: RATIO(maxNum: 14), height: RATIO_H(maxNum: 25)).width ?? 0)
        }
        
        lineView.snp.remakeConstraints { (make) in
            make.top.equalTo(bankImageView.snp.bottom).offset(RATIO_H(maxNum: 10))
            make.bottom.equalTo(highestLabel).offset(RATIO_H(maxNum: 20))
            make.width.equalTo(1)
            make.left.equalTo(highestLabel.snp.right).offset(RATIO(maxNum: 20))
        }
        
        millionLabel.snp.remakeConstraints { (make) in
            make.right.equalToSuperview().offset(RATIO(maxNum: -10))
            make.bottom.height.equalTo(amountLabel)
            make.width.equalTo((millionLabel.text?.size(font: RATIO(maxNum: 16), height: RATIO_H(maxNum: 25)).width)!-8)
        }
        
        interestLabel.snp.remakeConstraints { (make) in
            make.right.equalTo(millionLabel.snp.left)
            make.bottom.height.equalTo(millionLabel)
            make.width.equalTo((interestLabel.text?.size(font: RATIO(maxNum: 18), height: RATIO_H(maxNum: 25)).width)!-8)
        }
        
        dayLabel.snp.remakeConstraints { (make) in
            make.top.equalTo(interestLabel.snp.bottom)
            make.width.greaterThanOrEqualTo(10)
            make.height.equalTo(interestLabel)
            make.centerX.equalTo(interestLabel.snp.right)
        }
        
        workLabel.snp.remakeConstraints { (make) in
            
            make.height.equalTo(RATIO_H(maxNum: 60))
            make.top.equalTo(lineView)
        make.left.equalTo(lineView.snp.right).offset(RATIO(maxNum: 10))
        make.right.equalTo(interestLabel.snp.left).offset(RATIO(maxNum: -10))
        }
        
        lenderLabel.snp.remakeConstraints { (make) in
            make.left.equalTo(workLabel)
            make.bottom.equalTo(lineView)
            make.height.equalTo(RATIO_H(maxNum: 20))
            make.width.equalTo((lenderLabel.text?.size(font: RATIO(maxNum: 14), height: RATIO_H(maxNum: 20)).width)!)
        }
        
        mortgageLabel.snp.remakeConstraints { (make) in
            make.left.equalTo(lenderLabel.snp.right).offset(RATIO(maxNum: 5))
            make.height.centerY.equalTo(lenderLabel)
            make.width.equalTo((mortgageLabel.text?.size(font: RATIO(maxNum: 14), height: RATIO_H(maxNum: 20)).width)!)
        }
        
        lineView2.snp.remakeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
        
        self.layoutIfNeeded()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    

}
